package com.ag.wsq.service.impl;

import java.util.List;

import org.apache.log4j.Logger;

import com.ag.bean.TbConnectLog;
import com.ag.common.PageUtil;
import com.ag.wsq.dao.ConnectLogDAO;
import com.ag.wsq.dao.UtilDAO;
/*
 *对交接班日志的操作
 *@author 王淑其
 * 
 */
public class ConnectLogServiceImpl {

	private static final Logger logger = Logger.getLogger(ConnectLogServiceImpl.class);
	private ConnectLogDAO connectLogDAO;
	private UtilDAO utilDAO;
	/*
	 * 保存交接班日志
	 * @param connectLog
	 * 
	 */
	public void recordLog(TbConnectLog connectLog){		
		connectLogDAO.save(connectLog);
	}

	/*
	 * 查询交接班日志
	 * @param ...
	 * 
	 */
	public PageUtil WatchLog(int total, int page, int maxRow, String UId, String UName, String timeStart, String timeEnd){
		String cql = "select count(*) from TbConnectLog c where c.a is null";
		String hql = "from TbConnectLog c join fetch c.tbUser where c.a is null";
		
		if (UId!=null&&UId.length() > 0){
			cql += " and c.id.UId = " + UId;
			hql += " and c.id.UId = " + UId;
		}
		if (UName!=null&& UName.length() > 0){
			cql += " and c.tbUser.UName = " + UName;
			hql += " and c.tbUser.UName = " + UName;
		}
		if (timeStart != null && timeStart.length() > 0) {
			cql += " and c.id.startTime >= :timeStart";
			hql += " and c.id.startTime >= :timeStart";
		}
		if (timeEnd != null && timeEnd.length() > 0) {
			cql += " and c.id.startTime <= :timeEnd";
			hql += " and c.id.startTime <= :timeEnd";
		}
		hql += " order by c.id.startTime desc";
		PageUtil pageUtil = new PageUtil();
		List result = null;
		try {
			total = ((Long) this.utilDAO.queryCount(cql, timeStart, timeEnd).iterator().next()).intValue();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
System.out.println("------------------------------------");		
System.out.println("result size = " + total);
		
		if (page == 0) {
			page = 1;
		}
		if (maxRow == 0) {
			maxRow = 10;
		}
		int firstRow = (page - 1) * maxRow;
		try {
			result = this.utilDAO.query(hql, timeStart, timeEnd, firstRow, maxRow);
System.out.println("------------------------------------");	
System.out.println("result  = " + result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		pageUtil.setTotal(total);
		pageUtil.setPage(page);
		pageUtil.setMaxRow(maxRow);
		pageUtil.setList(result);
		return pageUtil;
	}
	
	public ConnectLogDAO getConnectLogDAO() {
		return connectLogDAO;
	}

	public void setConnectLogDAO(ConnectLogDAO connectLogDAO) {
		this.connectLogDAO = connectLogDAO;
	}

	public UtilDAO getUtilDAO() {
		return utilDAO;
	}

	public void setUtilDAO(UtilDAO utilDAO) {
		this.utilDAO = utilDAO;
	}
}
